﻿namespace SPTaxonomyClient.Taxonomy
{
	using System.Collections.Generic;
	using System.Text;

	/// <summary>
	/// A collection of terms that will be assigned as a field value of a File item
	/// </summary>
	public class TaxonomyFieldValueCollection
	{
		private IList<TaxonomyFieldValue> _values;

		/// <summary>
		/// A list of <see cref="TaxonomyFieldValue"/>
		/// </summary>
		public IList<TaxonomyFieldValue> Values
		{
			get { return _values ?? (_values = new List<TaxonomyFieldValue>()); }
		}

		/// <summary>
		/// Returns a textual representation of the TaxonomyFieldValues collection. This does the main part that allows
		/// assigning tags to a File item.
		/// </summary>
		/// <returns>A value composed of elements like &lt;int&gt;;#&lt;label&gt;|&lt;guid&gt; separated by ;# delimiters</returns>
		public override string ToString()
		{
			StringBuilder sb = new StringBuilder();
			
			for (int idx = 0, len = Values.Count; idx < len; ++idx)
			{
				TaxonomyFieldValue value = Values[idx];
				sb.Append(value);
				if (idx != len - 1)
				{
					sb.Append(";#");
				}
			}
			return sb.ToString();
		}
	}
}
